package com.zhuxulin.nokia;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class Message implements CommandListener {
	private Display dis = null;
	private List msgList = null;
	public Command backCMD;
	private Displayable parent;
	private String loginName;
	private String passwordP;
	private String msg;

	public Message(Display dis, Displayable parent, String loginName, String password, String msg) {
		this.dis = dis;
		this.parent = parent;
		this.loginName = loginName;
		this.passwordP = password;
		this.msg = msg;
		init();
	}

	private void init() {
		if (this.dis != null) {
			if (this.msg.indexOf("error:") == -1) {
				String[] msgArr = StringUtils.split(this.msg, "@");
				try {
					int size = msgArr.length;
					Order order = new Order();
					order.setOrderDate(msgArr[1].trim());
					order.setWinningDate(msgArr[2].trim());
					order.setCompany(msgArr[3].trim());
					if (size == 11) {
						order.setNumber1(msgArr[4].trim());
						order.setNumber2(msgArr[5].trim());
						order.setNumber3(msgArr[6].trim());
						order.setNumber4(msgArr[7].trim());
						order.setNumber5(msgArr[8].trim());
						order.setAggregateAmount(msgArr[9].trim());
						order.setOrderId(msgArr[10].trim());
					} else if (size == 10) {
						order.setNumber1(msgArr[4].trim());
						order.setNumber2(msgArr[5].trim());
						order.setNumber3(msgArr[6].trim());
						order.setNumber4(msgArr[7].trim());
						order.setNumber5("");
						order.setAggregateAmount(msgArr[8].trim());
						order.setOrderId(msgArr[9].trim());
					} else if (size == 9) {
						order.setNumber1(msgArr[4].trim());
						order.setNumber2(msgArr[5].trim());
						order.setNumber3(msgArr[6].trim());
						order.setNumber4("");
						order.setNumber5("");
						order.setAggregateAmount(msgArr[7].trim());
						order.setOrderId(msgArr[8].trim());
					} else if (size == 8) {
						order.setNumber1(msgArr[4].trim());
						order.setNumber2(msgArr[5].trim());
						order.setNumber3("");
						order.setNumber4("");
						order.setNumber5("");
						order.setAggregateAmount(msgArr[6].trim());
						order.setOrderId(msgArr[7].trim());
					} else if (size == 7) {
						order.setNumber1(msgArr[4].trim());
						order.setNumber2("");
						order.setNumber3("");
						order.setNumber4("");
						order.setNumber5("");
						order.setAggregateAmount(msgArr[5].trim());
						order.setOrderId(msgArr[6].trim());
					}

					RecordStore rs = RecordStore.openRecordStore("ordersDB", true);
					byte[] orderByte = order.serialize();
					int recordId = rs.addRecord(orderByte, 0, orderByte.length);
					rs.closeRecordStore();
				} catch (RecordStoreException ex) {
					ex.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				this.msgList = new List("Information", 3, msgArr, null);
				this.backCMD = new Command("Back", 2, 1);
				this.msgList.addCommand(this.backCMD);
				this.msgList.setCommandListener(this);
				this.dis.setCurrent(this.msgList);
			} else {
				Alert alert = new Alert("Warm Tips", this.msg.substring(6, this.msg.length()), null, AlertType.INFO);
				alert.setTimeout(3000);
				this.dis.setCurrent(alert);
			}
		}
	}

	public void commandAction(Command cmnd, Displayable dsplbl) {
		if (cmnd == this.backCMD)
			new BuyLottery(this.dis, this.parent, this.loginName, this.passwordP, "");
	}
}